package com.cy.pj.goods.controller;

import com.cy.pj.goods.pojo.Goods;
import com.cy.pj.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author Cx
 * @stage secondStageOfWeb
 * @creat 2020-11-04 20:31
 * Controller层:Controller层负责具体的业务模块流程的控制，
 * 在此层里面要调用Service层的接口来控制业务流程，
 * 控制的配置也同样是在Spring的配置文件里面进行，针对具体的业务流程，会有不同的控制器，
 * 我们具体的设计过程中可以将流程进行抽象归纳，设计出可以重复利用的子单元流程模块，
 * 这样不仅使程序结构变得清晰，也大大减少了代码量。
 */
@Controller
@RequestMapping("/goods/")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @RequestMapping("doGoodsUI")
    public String doGoodsUI(Model model) {
        List<Goods> list = goodsService.findGoods();
        model.addAttribute("list", list);
        return "goods";
    }

    @RequestMapping(value = {"doGoodsUIByName/{name}", "doGoodsUIByName"}) /* rest 风格 */
    public String doGoodsUIByName(@PathVariable(required = false) String name, Model model) {
        List<Goods> list = goodsService.findGoodsByName(name);
        model.addAttribute("list", list);
        return "goods";
    }

    @RequestMapping("doDeleteById/{id}")
    public String doDeleteById(@PathVariable Integer id) {
        int rows = goodsService.deleteById(id);
        return "redirect:/goods/doGoodsUI";
    }


    @RequestMapping("doSaveGoods")
    public String doSaveGoods(Goods entity) {
        goodsService.saveGoods(entity);
        return "redirect:/goods/doGoodsUI";
    }

    @RequestMapping("doGoodsAddUI")
    public String doGoodsAddUI() {
        return "goods-add";
    }


    @RequestMapping("doFindById/{id}")
    public String doFindById(@PathVariable Integer id, Model model) {
        Goods goods = goodsService.findById(id);
        model.addAttribute("goods", goods);
        return "goods-update";
    }

    @RequestMapping("doUpdateGoods")
    public String doUpdateGoods(Goods goods) {
        goodsService.updateGoods(goods);
        return "redirect:/goods/doGoodsUI";
    }

}
